package com.yuan.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuan.reggie.common.CustomerException;
import com.yuan.reggie.common.R;
import com.yuan.reggie.dto.SetmealDto;
import com.yuan.reggie.pojo.Category;
import com.yuan.reggie.pojo.Dish;
import com.yuan.reggie.pojo.Setmeal;
import com.yuan.reggie.pojo.SetmealDish;
import com.yuan.reggie.service.CategoryService;
import com.yuan.reggie.service.SetmealDishService;
import com.yuan.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yuanxingke
 * @date 2022/8/2 17:11
 */
@Slf4j
@RestController
@RequestMapping("setmeal")
public class SetmealController {


    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;


    @Autowired
    private SetmealDishService setmealDishService;

    @PostMapping
    @CacheEvict(cacheNames = "setmealCache",key = "#dishList.categoryId")
    public R<String> save(@RequestBody SetmealDto dishList){
        setmealService.saveWithDish(dishList);
        return R.success("保存成功");
    }

    @GetMapping("page")
    public R<Page<SetmealDto>> page(Integer page,Integer pageSize,String name){

        //1、查询套餐的名字对应的数据
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(name),Setmeal::getName,name).orderByDesc(Setmeal::getCreateTime);
        setmealService.page(setmealPage,lambdaQueryWrapper);

        //复制 并且去除
        Page<SetmealDto> setmealDtoPage = new Page<>();
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");

        //2、根据数据查询套餐所对于的菜品名称
        List<SetmealDto> dtoList=new ArrayList<>();
        List<Setmeal> records = setmealPage.getRecords();
        for (Setmeal setmeal : records) {
            //查询出来每一个
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            Category category = categoryService.getById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            dtoList.add(setmealDto);
        }
        setmealDtoPage.setRecords(dtoList);
        return R.success(setmealDtoPage);
    }


    @DeleteMapping()
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public R<String> delete(Long[] ids){
        if(ids.length == 0){
            throw new CustomerException("你未选择菜品");
        }
        setmealService.removeWithDish(ids);
        return R.success("删除成功");
    }
    /**
     * 查询套餐信息
     *
     */

    @GetMapping("list")
    @Cacheable(cacheNames = "setmealCache",key = "#setmeal.categoryId",unless = "#result.data.size()==0")
    public R<List<Setmeal>> list(Setmeal setmeal){

        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(setmeal.getStatus() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        lambdaQueryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        lambdaQueryWrapper.orderByDesc(Setmeal::getCreateTime);
        List<Setmeal> setmealList = setmealService.list(lambdaQueryWrapper);

        return R.success(setmealList);
    }
    @PostMapping("status/{num}")
    public R<String> status(@PathVariable("num") Integer num, Long[] ids){

        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(num);
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Setmeal::getId,ids);
        setmealService.update(setmeal,lambdaQueryWrapper);
        return R.success("停售成功");
    }

    @GetMapping("{id}")
    public R<SetmealDto> getById(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getByIdWithDish(id);
        return R.success(setmealDto);
    }

    @PutMapping
    @Transactional
    public R<String> update(@RequestBody SetmealDto setmealDto){

        //更新主表
        setmealService.updateById(setmealDto);
        //更新从表
        //先删除再更新
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(lambdaQueryWrapper);

        //插入数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        //需要设置id
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));

        setmealDishService.saveBatch(setmealDishes);


        return R.success("更新成功");
    }



}
